package orest.back.inforecommend;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import orest.back.userneeds.UserNeedsHandler;
import orest.back.userneeds.UserNeedsSql;

import net.ssta.common.Constants;
import net.ssta.common.LoginUser;
import net.ssta.core.base.BaseException;
import net.ssta.core.base.ErrorCode;
import net.ssta.core.base.HandlerBase;
import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.DataBaseExecutor;
import net.ssta.core.db.PagingInfo;
import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
import net.ssta.core.web.RequestKit;
import net.ssta.core.util.DateUtility;


/**
 * 
 * @ClassName: IllegalInfoRecordHandler
 * @Description: 信息推荐管理
 * @author:
 * @date:
 * 
 */
public class InfoRecommendHandler extends HandlerBase
{
    
    /**
     * 获取成果信息列表
     */
    public RecordSet kjcgInfoList(RequestKit requestKit, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = InfoRecommendSql.getKjcgInfoList(requestKit);
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
    /**
     * 科技成果推荐
     */
    public void changeKjcgStatus(RequestKit requestKit)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	
        	String kjcgUuids = requestKit.getParameter("kjcgUuids", "");
        	String fieldname = requestKit.getParameter("fieldName", "");
        	String status = requestKit.getParameter("status", "");
        	
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String kjcguuidArr[] = kjcgUuids.split(",");
        	if(kjcguuidArr!=null&&kjcguuidArr.length>0){
	            String sql = InfoRecommendSql.changeKjcgStatus(kjcgUuids,fieldname,status);
	            m_executor.execute(sql);
        	}
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
    
    
    
    /**
     * 获取专家信息列表
     */
    public RecordSet ExpertPhotoMaintainList(RequestKit requestKit, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = InfoRecommendSql.getExpertPhotoMaintainList(requestKit);
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
    
    
    
    
    
    /**
     * 获取专家信息列表
     */
    public RecordSet ExpertInfoList(RequestKit requestKit, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = InfoRecommendSql.getExpertInfoList(requestKit);
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
    
    /**
     * 专家推荐
     */
    public void changeExpertStatus(RequestKit requestKit)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	
        	String expertUuids = requestKit.getParameter("expertUuids", "");
        	String fieldname = requestKit.getParameter("fieldName", "");
        	String status = requestKit.getParameter("status", "");
        	
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String expertUuidArr[] = expertUuids.split(",");
        	if(expertUuidArr!=null&&expertUuidArr.length>0){
	            String sql = InfoRecommendSql.changeExpertStatus(expertUuids,fieldname,status);
	            m_executor.execute(sql);
        	}
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
    
    /**
     * 设置推荐到频道的专家的排序号
     */
    public void setSortNum(String tableName,String uuidFiledName,String uuid,String sortField,String sortNum)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            
            if(!tableName.equals("")&&!uuidFiledName.equals("")&&!sortField.equals("")&&!uuid.equals("")){
	            String sqlstr = "UPDATE "+ tableName+" SET "+ sortField + "='"+sortNum+"' WHERE "+ uuidFiledName + "='"+uuid+"'";
	            m_executor.execute(sqlstr);
            }
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
    
    
    
    /**
     * 获取单位信息列表
     */
    public RecordSet unitInfoList(RequestKit requestKit, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = InfoRecommendSql.getUnitInfoList(requestKit);
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
    /**
     * 科研单位推荐
     */
    public void changeUnitStatus(RequestKit requestKit)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	
        	String unitUuids = requestKit.getParameter("unitUuids", "");
        	String fieldname = requestKit.getParameter("fieldName", "");
        	String status = requestKit.getParameter("status", "");
        	
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String unitUuidArr[] = unitUuids.split(",");
        	if(unitUuidArr!=null&&unitUuidArr.length>0){
	            String sql = InfoRecommendSql.changeUnitStatus(unitUuids,fieldname,status);
	            m_executor.execute(sql);
        	}
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
}
